package com.hby.game

import android.app.ActivityManager
import android.graphics.Paint
import android.os.Debug
import android.view.MotionEvent
import com.daidai.tools.G
import game.engine.base.*
import game.engine.base.anim.single.MoveTo
import game.engine.base.anim.call
import game.engine.base.anim.scaleTo

class GameScene : Scene() {

    var allGameEnd = false
    var elapsedTime = 0f
    var hbCreatorEnd = false

    val lstHb = ArrayList<HbSprite>()

    lateinit var lbCoin: Label
    lateinit var lbTime: Label
    lateinit var spCoin: Sprite

    var coins = 0
    var coinPos = Vec2()
    override fun onTouch(touch: Touch) {
        if (touch.type == MotionEvent.ACTION_DOWN) {

//            UDebug.info("touch === x:${touch.x},y:${touch.y}")

            // 判断哪个被点击了
            // 需要分层级,先检查后面的
            for (i in lstHb.size - 1 downTo 0) {
                val hb = lstHb[i]
                if (hb.getBoundingBox().contains(touch.x, touch.y)) {
                    hb.removeFromParent()
                    lstHb.removeAt(i)

                    // 一个金币飞过去
                    val graphics = Director.instance.graphics
                    val coin = Sprite(bitmap = graphics.coinBitmap).apply {
                        x = touch.x
                        y = touch.y
                        zorder = 1
                    }
                    addChild(coin)

                    MoveTo(0.5f, coinPos.x, coinPos.y)
                            .scaleTo(0.2f, 1.2f, 1.2f)
                            .scaleTo(0.2f, 1.0f, 1.0f)
                            .call {
                                coin.removeFromParent()
                            }.runBy(coin)


                    coins++
                    lbCoin.text = "$coins"
                    return
                }
            }
        }
    }

    override fun onEnter() {
        super.onEnter()

        // 显示一个全局的背景
        val director = Director.instance
        val graphics = director.graphics
        val wa = director.windowAdapter
        fillBgBitmap = graphics.bgBitmap

        // info
        val coinInfo = Sprite(bitmap = graphics.infoBgBitmap).apply {
            x = wa.minX + 140f
            y = wa.minY + HbConfig.InfoOffsetY

            // coin
            val coin = Sprite(bitmap = graphics.coinBitmap)
            coin.x = -100f
            addChild(coin)
            spCoin = coin

            // text
            val text = Label("0")
            text.x = 10f
            text.align = Paint.Align.CENTER
            addChild(text)
            lbCoin = text
        }
        addChild(coinInfo)

        val timeInfo = Sprite(bitmap = graphics.infoBgBitmap).apply {
            x = wa.maxX - 140f
            y = wa.minY + HbConfig.InfoOffsetY

            // time
            val time = Sprite(
                    bitmap = graphics.timeBitmap,
                    x = -100f
            )
            addChild(time)

            val timeText = Label("倒计时:10s", align = Paint.Align.CENTER)
            addChild(timeText)
            lbTime = timeText
        }
        addChild(timeInfo)

        // 进行10s 开始倒计时
        val count = 10
        scheduleCount(1f, count, 1f) { countIdx ->
            if (countIdx >= count - 1) {
                hbCreatorEnd = true
            }
            lbTime.text = "倒计时:${count - countIdx - 1}s"
        }

        coinPos = spCoin.getWorldPos()
        UDebug.info("coinPos:${coinPos.x},${coinPos.y}")
    }

    override fun update(dt: Float) {

        if (allGameEnd) return

        // 先更新
        val director = Director.instance
        val wa = director.windowAdapter
        val iter = lstHb.iterator()
        while (iter.hasNext()) {
            val hb = iter.next()
            hb.y += hb.speed * dt
            if (hb.y >= wa.maxY + hb.height / 2f) {
                iter.remove()
            }
        }

        if (hbCreatorEnd && lstHb.size < 1) {
            // 弹出结果框
            allGameEnd = true
            director.gameApp.showResult()
        }

        // 产生一堆红包
        if (!hbCreatorEnd) {
            elapsedTime += dt
            if (elapsedTime >= 0.25f) {
                elapsedTime = 0f
                // 产生一个红包
                val hb = genHb()
                addChild(hb)
                lstHb.add(hb)
            }
        }
    }

    fun genHb(): HbSprite {
        val speed = CoreUtil.randomFloat(400f, 600f)
        val wa = Director.instance.windowAdapter
        val graphics = Director.instance.graphics
        return HbSprite(bitmap = graphics.hbBitmap1, speed).apply {
            x = CoreUtil.randomFloat(wa.minX + width / 2f, wa.maxX - width / 2f)
            y = wa.minY + height / 2f + 130f
        }
    }

}